##
## make for os

NASM=nasm
NASM_OP=

GCC=gcc
LD=ld

IMAGE=snaky.img

OBJDIR=.obj

include_dir=-Ilib -Ikernel

all: image

mbr.bin: boot/mbr.S
	$(NASM) $(NASM_OP) -I boot/include/ -I boot/ -o $@ $^

kernel.bin:
	@mkdir -p $(OBJDIR)
	$(NASM) $(NASM_OP) -f elf -o $(OBJDIR)/print.o kernel/print.S
	$(GCC) $(include_dir) -m32 -c -o $(OBJDIR)/main.o kernel/main.c
	$(LD) -m elf_i386 -Ttext 0xc0001500 -e main -o $@ $(OBJDIR)/main.o $(OBJDIR)/print.o

loader.bin: boot/loader.S
	$(NASM) $(NASM_OP) -Iboot/include/ -o $@ $^

image: baseos mbr.bin loader.bin kernel.bin
	dd if=mbr.bin of=$(IMAGE) count=1 bs=512 conv=notrunc
	dd if=loader.bin of=$(IMAGE) count=3 bs=512 seek=2 conv=notrunc
	dd if=kernel.bin of=$(IMAGE) count=200 bs=512 seek=9 conv=notrunc

baseos:
	dd if=/dev/zero of=$(IMAGE) count=512 bs=20480

sys: syscall_write

syscall_write: syscall/syscall_write.S
	$(NASM) $(NASM_OP)  -o $@ $^

clean:
	rm -rf mbr.bin loader.bin kernel.bin
	rm -rf syscall_write
	rm -rf snaky.img
	rm -rf $(OBJDIR)


